var parentIds=0;
$(function () {
    getTree();
});
/*重置*/
function resetClick(){
    $(".dis-inebl input").val("");
    $("#menus option:selected").val("全部");
    $("#userState option:selected").val("全部");
    getUserList();
}
function getTree(){
    JY.Model.loading();
    ym.ajax({
        url : "/shop/cat/get",
        type : "post",
        param:{
            "parentId":0
        },
        success : function(data) {
            console.log(data)
            console.log("获取树形菜单")
            var zNodes=data.obj;
            //设置
            var setting = {view:{dblClickExpand: false,selectedMulti:false,nameIsHTML:true},data:{simpleData:{enable:true}},callback:{onClick:onClickTree}};
            $.fn.zTree.init($("#preResourcesTree"), setting, zNodes);
            JY.Model.loadingClose();
        }
    })
    getUserList();
}
function onClickTree(e, treeId, treeNode) {
    parentIds=0;
    var zTree = $.fn.zTree.getZTreeObj("preResourcesTree"),
        nodes = zTree.getSelectedNodes(),v ="",n ="",p="";
    for (var i=0, l=nodes.length; i<l; i++) {
        v += nodes[i].name + ",";//获取name值
        n += nodes[i].id + ",";//获取id值
        var pathNodes=nodes[i].getPath();
        for(var y=0;y<pathNodes.length;y++){
            p+=pathNodes[y].name+"/";//获取path/name值
        }
    }
    if (v.length > 0 ) v = v.substring(0, v.length-1);
    if (n.length > 0 ) n = n.substring(0, n.length-1);
    if (p.length > 0 ) p = p.substring(0, p.length-1);
    $("#preResources").prop("value",p);
    $("#auForm input[name$='parentId']").prop("value",n);
    parentIds=n;
    //因为单选选择后直接关闭，如果多选请另外写关闭方法
    hidePreResources();
}
var preisShow=false;//窗口是否显示
function showPreResources() {
    if(preisShow){
        hidePreResources();
    }else{
        var obj = $("#preResources");
        var offpos = $("#preResources").position();
        $("#preResourcesContent").css({left:offpos.left+"px",top:offpos.top+obj.heith+ "px"}).slideDown("fast");
        preisShow=true;
    }
}
function hidePreResources(){
    $("#preResourcesContent").fadeOut("fast");
    preisShow=false;
}
/*查询*/
function getUserList(init){
    JY.Model.loading();
    var pageNum;
    var pageSize
    if(init!=1){
        pageNum  =$("#baseForm .pageNum").val();
        pageSize  =$("#baseForm .pageSize").val();
    }
	$("#ticktTable tbody").html("");
	var textName =$("#textName").val().trim();
	var userState=$("#userState option:selected").val();
    if(userState=="全部"){
        userState="";
    }
	ym.ajax({
		url : "/shop/cat/conditionalQuery",
		type : "get",
        param:{
            "pageNum":pageNum,
            "pageSize":pageSize,
            "text":textName,
            "state":userState,
		},
		jsonType : "json",
		success : function(data) {
		    console.log(data)
            console.log("获取商城信息")
            var data=data instanceof Object ? data : JSON.parse(data);
            var html="";
            var list=data.obj;
            var results=list.list;
            var pageNum=list.pageNum,
                pageSize=list.pageSize,
                totalRecord=list.totalRecord;
           if(results!=null&&results.length>0){
                var leng=(pageNum-1)*pageSize;//计算序号
                for(var i = 0;i<results.length;i++){
                    var l=results[i];
                    var jsonL=JSON.stringify(l);
                    html+="<tr class='tr_color'>";
                    html+="<td class='center ver-m'><label><input type='checkbox' name='ids' value='"+jsonL+"' class='ace' /> <span class='lbl'></span></label></td>";
                    html+="<td class='center'>"+l.text+"</td>";
                    html+="<td class='center'>"+l.sortOrder+"</td>";
                    if(l.status==1){
                        html+="<td class='center'>有效</td>";
                    }else if(l.status==0){
                        html+="<td class='center'>无效</td>";
                    }
                    html+="</tr>";
                }
                $("#ticktTable tbody").append(html);
                JY.Page.setPage("baseForm","pageing",pageSize,pageNum,totalRecord,"getUserList");
            }else{
                html+="<tr><td colspan='4' class='center'>没有相关数据</td></tr>";
                $("#ticktTable tbody").append(html);
                $("#baseForm ul").empty();//清空分页
            }
            JY.Model.loadingClose();
		}
	})
}
//新增
function addAri(){
    clearForm();
    JY.Model.edit("auForm_edit","新增商品类目",function(){
        $("#preResourcesTree").html("");
        var that=$(this);
        if(JY.Validate.form("generateForm")) {
            $(".masks").removeClass("hide");
            var datas={};
            datas.text=$("#generateForm input[name='textName']").val().trim();
            datas.parentId=parentIds;
            datas.status=$("#status").find("option:selected").val();
            datas.sortOrder=$("#generateForm input[name='sortOrder']").val().trim();
            ym.ajax({
                url: "/shop/cat/add",
                type: "post",
                param:{
                    "cat":JSON.stringify(datas),
                },
                success: function (data) {
                    console.log(data)
                    console.log("新增");
                    that.dialog("close");
                    $(".masks").addClass("hide");
                    JY.Model.info("操作成功!",function(){getTree();});
                }
            })
        }
    })
}
//编辑
function editAri(){
    clearForm();
    var chks =[];
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
    });
    if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length==1){
        var obj=JSON.parse(chks);
        $("#generateForm input[name='textName']").val(obj.text);
        $("#generateForm input[name='sortOrder']").val(obj.sortOrder);
        $("#status").find("option[value = '"+obj.status+"']").prop("selected","selected");
        if(obj.parentId!=0){
            $("#preResources").val(obj.parentName);
        }
        JY.Model.edit("auForm_edit","编辑商品类目",function(){
            $("#preResourcesTree").html("");
            var that=$(this);
            if(parentIds==obj.id){
                JY.Model.info("本身不能作为父类!");
            }else{
                JY.Model.loading();
                var datas={};
                datas.id=obj.id;
                datas.text=$("#generateForm input[name='textName']").val().trim();
                if(parentIds!=0){
                    datas.parentId=parentIds;
                }else{
                    datas.parentId=obj.parentId;
                }
                datas.status=$("#status").find("option:selected").val();
                datas.sortOrder=$("#generateForm input[name='sortOrder']").val().trim();
                ym.ajax({
                    url: "/shop/cat/update",
                    type: "post",
                    param:{
                        "cat":JSON.stringify(datas),
                    },
                    success: function (data) {
                        console.log(data)
                        console.log("编辑")
                        JY.Model.loadingClose();
                        that.dialog("close");
                        JY.Model.info("操作成功!",function(){getTree();});
                    }
                })
            }
        })
    }else if(chks.length>1){
        JY.Model.info("一次只能编辑一条信息!");
    }
}
// 启用禁用
function enAbleClick(type){
    var chks =[];
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
    });
    if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length>0){
        var states="";
        if(type==1){
            //启用
            states=1;
        }else if(type==0){
            //禁用
            states=0;
        }
        JY.Model.message("auForm_edits","修改状态",function(){
            var that = $(this);
            ym.ajax({
                url : "/shop/cat/del",
                type : "POST",
                param:{
                    "status":states,
                    "ids":chks,
                },
                jsonType : "json",
                success : function(data) {
                   if(data.res==1){
                       that.dialog("close");
                       JY.Model.info("操作成功!",function(){getUserList();});
                   }
                }
            })
        });
    }
}
function clearForm(){
    $("#generateForm input").val("");
    $("#status").find("option[value = '1']").prop("selected","selected");
}